package com.glenet.salary.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.glenet.salary.entities.HrPreDetail;
import com.glenet.salary.util.SessionFactoryUtil;

public class HrPreDetailDao extends HrDaoSuport {

	@SuppressWarnings("unchecked")
	public List<HrPreDetail> list(int offset, int limit) {

		List<HrPreDetail> list = new ArrayList<HrPreDetail>();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			list = (List<HrPreDetail>) session.selectList("HrPreDetail.select", null, new RowBounds(offset, limit));
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public int batchImport(List<HrPreDetail> list) {
		int effect = 0;
		try {
			SqlSession session = SessionFactoryUtil.getSessionFactory().openSession(ExecutorType.BATCH);
			for (HrPreDetail preDetail : list) {
				session.insert("HrPreDetail.insert", preDetail);
			}
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return effect;
	}

	@SuppressWarnings("unchecked")
	public List<HrPreDetail> getPreDetails(Long companyId) {

		List<HrPreDetail> list = new ArrayList<HrPreDetail>();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			list = (List<HrPreDetail>) session.selectList("HrPreDetail.getPreDetails", companyId);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public HrPreDetail getPreDetail(Long employeeId) {
		HrPreDetail preDetail = new HrPreDetail();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			preDetail = (HrPreDetail) session.selectOne("HrPreDetail.getPreDetail", employeeId);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return preDetail;
	}

}
